/**
 * Project Wonderland
 *
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * Sun designates this particular file as subject to the "Classpath"
 * exception as provided by Sun in the License file that accompanied
 * this code.
 */
package org.jdesktop.wonderland.modules.season.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.jdesktop.wonderland.client.cell.properties.CellPropertiesEditor;
import org.jdesktop.wonderland.client.cell.properties.annotation.PropertiesFactory;
import org.jdesktop.wonderland.client.cell.properties.spi.PropertiesFactorySPI;
import org.jdesktop.wonderland.common.cell.state.CellComponentServerState;
import org.jdesktop.wonderland.common.cell.state.CellServerState;
import org.jdesktop.wonderland.modules.season.common.SeasonCellComponentServerState;
import org.jdesktop.wonderland.modules.season.common.SeasonCellEnum;

/**
 * The property sheet for the Tooltip Cell Component.
 *
 * @author christian
 */
@PropertiesFactory(SeasonCellComponentServerState.class)
public class SeasonCellComponentProperties extends JPanel
        implements PropertiesFactorySPI {

    // The I18N resource bundle
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(
            "org/jdesktop/wonderland/modules/season/client/resources/Bundle");

    // The editor window
    private CellPropertiesEditor editor = null;            
    
    private SeasonCellEnum season;

    // The original value for the tooltip text, before any editing
    private String originalText = null;

    // The original value for the tooltip timeout, -1 if there is no timeout
    private int originalTimeout = -1;

    // The initial default timeout to use
    private static final int DEFAULT_TIMEOUT = 2000;
    
    /** Creates new form TooltipComponentProperties */
    public SeasonCellComponentProperties() {
        // Initialize the GUI
        initComponents();
    }

    /**
     * @inheritDoc()
     */
    public String getDisplayName() {
        return BUNDLE.getString("Season_Cell_Component");
    }

    /**
     * @inheritDoc()
     */
    public JPanel getPropertiesJPanel() {
        return this;
    }

    /**
     * @inheritDoc()
     */
    public void setCellPropertiesEditor(CellPropertiesEditor editor) {
        this.editor = editor;
    }

    /**
     * @inheritDoc()
     */
    public void open() {
        // Fetch the tooltip component state from the Cell server state.
        CellServerState state = editor.getCellServerState();
        CellComponentServerState compState = state.getComponentServerState(
                SeasonCellComponentServerState.class);

        // If there is a tooltip component server state (there should be), then
        // populate its values in the GUI.
        if (state != null) {
            SeasonCellComponentServerState tss =
                    (SeasonCellComponentServerState) compState;

            // Store away the tooltip text and update the GUI
            season = tss.getSeason();
            
            switch(season) {
                case WINTER:
                    seasonWinter.setSelected(true);
                    break;
                case SPRING:
                    seasonSpring.setSelected(true);
                    break;
                case SUMMER:
                    seasonSummer.setSelected(true);
                    break;
                case AUTUMN:
                    seasonAutumn.setSelected(true);
                    break;
                default:
                    seasonSpring.setSelected(true);
                    break;
            }
        }
    }

    /**
     * @inheritDoc()
     */
    public void close() {
        // Do nothing for now.
    }

    /**
     * @inheritDoc()
     */
    public void apply() {
        // Fetch the latest tooltip component server state from the Cell server
        // state.
        CellServerState state = editor.getCellServerState();
        CellComponentServerState compState = state.getComponentServerState(
                SeasonCellComponentServerState.class);

        // Update the tooltip text in the component server state
        SeasonCellEnum temp = null;
        if (seasonWinter.isSelected()) {
            temp = temp.WINTER;
        } else if (seasonSpring.isSelected()) {
            temp = temp.SPRING;
        } else if (seasonSummer.isSelected()) {
            temp = temp.SUMMER;
        } else if (seasonAutumn.isSelected()) {
            temp = temp.AUTUMN;
        }
        ((SeasonCellComponentServerState) compState).setSeason(temp);
        editor.addToUpdateList(compState);
    }

    /**
     * @inheritDoc()
     */
    public void restore() {
    }

    /**
     * Inner class to listen for changes to the text field and fire off dirty
     * or clean indications to the cell properties editor.
     */
    class InfoTextFieldListener implements DocumentListener {

        public void insertUpdate(DocumentEvent e) {
            checkDirty();
        }

        public void removeUpdate(DocumentEvent e) {
            checkDirty();
        }

        public void changedUpdate(DocumentEvent e) {
            checkDirty();
        }

        private void checkDirty() {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        seasonGroup = new javax.swing.ButtonGroup();
        seasonPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        seasonWinter = new javax.swing.JRadioButton();
        seasonSpring = new javax.swing.JRadioButton();
        seasonSummer = new javax.swing.JRadioButton();
        seasonAutumn = new javax.swing.JRadioButton();

        setLayout(new java.awt.GridBagLayout());

        seasonPanel.setLayout(new java.awt.GridLayout(5, 0));

        jLabel1.setText("Select current season:");
        seasonPanel.add(jLabel1);

        seasonGroup.add(seasonWinter);
        seasonWinter.setSelected(true);
        seasonWinter.setText("Winter");
        seasonWinter.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seasonWinterMouseClicked(evt);
            }
        });
        seasonPanel.add(seasonWinter);

        seasonGroup.add(seasonSpring);
        seasonSpring.setText("Spring");
        seasonSpring.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seasonSpringMouseClicked(evt);
            }
        });
        seasonPanel.add(seasonSpring);

        seasonGroup.add(seasonSummer);
        seasonSummer.setText("Summer");
        seasonSummer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seasonSummerMouseClicked(evt);
            }
        });
        seasonPanel.add(seasonSummer);

        seasonGroup.add(seasonAutumn);
        seasonAutumn.setText("Autumn");
        seasonAutumn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seasonAutumnMouseClicked(evt);
            }
        });
        seasonPanel.add(seasonAutumn);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(seasonPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void seasonWinterMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seasonWinterMouseClicked
        editor.setPanelDirty(SeasonCellComponentProperties.class, true);
    }//GEN-LAST:event_seasonWinterMouseClicked

    private void seasonSpringMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seasonSpringMouseClicked
        editor.setPanelDirty(SeasonCellComponentProperties.class, true);
    }//GEN-LAST:event_seasonSpringMouseClicked

    private void seasonSummerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seasonSummerMouseClicked
        editor.setPanelDirty(SeasonCellComponentProperties.class, true);
    }//GEN-LAST:event_seasonSummerMouseClicked

    private void seasonAutumnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seasonAutumnMouseClicked
        editor.setPanelDirty(SeasonCellComponentProperties.class, true);
    }//GEN-LAST:event_seasonAutumnMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton seasonAutumn;
    private javax.swing.ButtonGroup seasonGroup;
    private javax.swing.JPanel seasonPanel;
    private javax.swing.JRadioButton seasonSpring;
    private javax.swing.JRadioButton seasonSummer;
    private javax.swing.JRadioButton seasonWinter;
    // End of variables declaration//GEN-END:variables
}
